package org.moonlight.jvm.classfile.constant.info;

import org.moonlight.jvm.classfile.ClassReader;
import org.moonlight.jvm.classfile.constant.ConstantInfo;
import org.moonlight.jvm.classfile.constant.StaticConstantPool;

/**
 * CONSTANT_Class_info常量表示类或者接口的符号引用，结构如下：
 *   CONSTANT_Class_info {
 *       u1 tag
 *       u2 name_index
 *   }
 * 和 CONSTANT_String_info类似，name_index是常量池索引，指向CONSTANT_Utf8_info常量。
 *
 *  {@link ConstantInfo#CONSTANT_CLASS}
 *
 * @author Moonlight
 * @createTime 2023/8/15 14:44
 **/
public class ConstantClassInfo implements ConstantInfo {

    private final StaticConstantPool scp;
    private int nameIdx;

    public ConstantClassInfo(StaticConstantPool scp) {
        this.scp = scp;
    }

    @Override
    public void readInfo(ClassReader reader) {
        this.nameIdx = reader.readUint16();
    }

    @Override
    public int tag() {
        return ConstantInfo.CONSTANT_CLASS;
    }

    public String name() {
        return this.scp.getUtf8(this.nameIdx);
    }

}
